package jsoa.ceict.com.dlna;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.hezb.clingupnp.DlnaDeviceInfo;
import com.hezb.clingupnp.DlnaSearch;
import com.hezb.clingupnp.dmc.UpnpActionCallBack;
import com.hezb.clingupnp.dmc.UpnpControlSet;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

public class MainActivity extends AppCompatActivity {

    private DlnaSearch dlnaSearch;
    private ArrayList<DlnaDeviceInfo> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        dlnaSearch = new DlnaSearch(this);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        list.clear();
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                dlnaSearch.startSearchDMP();
                dlnaSearch.startSearchDMC();
            }
        });
    }
    public void onEventMainThread(DlnaDeviceInfo dlnaDeviceInfo) {
        list.add(dlnaDeviceInfo);
        Log.w("weitu", "onEventMainThread收到了消息：" + dlnaDeviceInfo.getDevice().getDetails().getFriendlyName());
    }

    public void play(View view) {
        for (DlnaDeviceInfo device : list) {
            if (device.getDevice().getDisplayString().contains("QQ")){
                continue;
            }
            UpnpControlSet upnpControlSet = dlnaSearch.createUpnpControlSet(device.getDevice());
            if (upnpControlSet != null) {
                String url = "http://baobab.wdjcdn.com/1455782903700jy.mp4";
                upnpControlSet.setAVTransportURI(url,1,"1455782903700jy.mp4");
                upnpControlSet.setUPnPActionCallBack(new UpnpActionCallBack() {
                    @Override
                    public void onSuccess(int type) {
                        Log.w("weitu","onSuccess:"+type);
                    }

                    @Override
                    public void onFailure(int type, String error) {
                        Log.w("weitu","onFailure:"+type+";"+error);
                    }

                    @Override
                    public void getVolumeReceived(int volume) {
                        Log.w("weitu","getVolumeReceived:"+volume);
                    }

                    @Override
                    public void getTransportReceived(boolean isPlaying) {
                        Log.w("weitu","getTransportReceived:"+isPlaying);
                    }

                    @Override
                    public void getPositionInfoReceived(int currentPosition, int duration) {
                        Log.w("weitu","getPositionInfoReceived:"+duration+";"+currentPosition);
                    }
                });
            } else {
                Toast.makeText(this,"远程设备不支持推屏",Toast.LENGTH_LONG).show();
            }
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
